using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.IO.Compression;
using System.Runtime.InteropServices;
using Shell32;

namespace DirectoryHelper
{
    public enum DirectoryContentType
    {
        Unknown = 0,
        Directory = 1,
        File = 2
    }

    public class DirectoryHelper
    {
        Stack<object> directories2Walk = new Stack<object>();
        
        public void WalkDirectory(TreeNode Node, string rootFolder)
        {
            if (Node != null)
            {
                do
                {
                    string[] Folders = Directory.GetDirectories(Node.Tag.ToString (), "*.*");

                    if (Folders != null)
                    {
                        foreach (var currFolder in Folders)
                        {
                            string currFolderName = currFolder.Replace (Path.GetDirectoryName (currFolder), string.Empty);
                            currFolderName = currFolderName.Replace("\\", string.Empty);
                            currFolderName = currFolderName.Replace("%20", " ");
                            TreeNode currFolderNode = Node.Nodes.Add (currFolderName);
                            currFolderNode.Tag = currFolder;
                            currFolderNode.ToolTipText = string.Concat (rootFolder, "\\", currFolderName);

                            directories2Walk.Push(currFolderNode);
                        }
                    }

                    if (directories2Walk.Count > 0)
                    {
                        WalkDirectory(directories2Walk.Pop() as TreeNode, rootFolder);
                    }

                    string[] Files = Directory.GetFiles(Node.Tag.ToString (), "*.*");

                    if (Files != null)
                    {
                        foreach (var currFile in Files)
                        {
                            string currFileName = Path.GetFileName (currFile);
                            currFileName = currFileName.Replace("\\", string.Empty);
                            currFileName = currFileName.Replace("%20", " ");
                            TreeNode currFileNode = Node.Nodes.Add (currFileName);
                            currFileNode.Tag = currFile;
                            currFileNode.ToolTipText = string.Concat(rootFolder, "\\", currFileName); ;
                        }
                    }

                } while (directories2Walk.Count > 0);
            }
        }

        public void ExportVSIX(TreeNode RootNode, string OutputPath)
        {
            string OriginalOutputPath = OutputPath;

            if (RootNode != null)
            {
                foreach (TreeNode currNode in RootNode.Nodes)
                {
                    if (currNode.Nodes.Count > 0)
                    {
                        try
                        {
                            string newOutputPath = Directory.CreateDirectory(Path.Combine(OutputPath, currNode.Text)).FullName;

                            ExportVSIX(currNode, newOutputPath);
                        }
                        catch { }
                    }
                    else
                        try
                        {
                            string srcFileName = currNode.Tag.ToString ();
                            File.Copy (srcFileName, Path.Combine (OutputPath, currNode.Text));
                        }
                        catch { }
                }
            }
        }
    }
}
